package com.boonya.springboot.watcher.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName: FileManagerController
 * @Description: TODO(功能说明)
 * @author: pengjunlin
 * @motto: 学习需要毅力，那就秀毅力
 * @date 2021/5/26 23:37
 */
@RestController
@RequestMapping("/file")
public class FileManagerController {

    @Value("${fileManagerPath:D:/}")
    String fileManagerPath;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public boolean upload(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return false;
        }
        File newFile = new File(fileManagerPath+"/"+file.getOriginalFilename());
        try {
            file.transferTo(newFile);
        }catch (IOException e){
            e.printStackTrace();
        }
        return true;
    }
}
